//sudo wget -c "url" -O filename.png -o filename.log -t trytimes --timeout=timeoutseconds
#ifndef MAP_H
#define MAP_H

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "./TinyXML/tinyxml.h"
#include "aks.h"

#define DRIVING 0
#define WALKING 1
#define TRANSIT 2
#define RIDING  3

class baiduMapStaticImage{
    private:
        int width, height;
        double cen_lo, cen_la;
        int zoom;
    public:
        void setCentral(double lo, double la){
            cen_lo = lo;
            cen_la = la;
        }
        void setImageStyle(int w, int h, int z){
            width = w;
            height = h;
            zoom = z;
        }
        int getStaticImage(const char* filename, int tryTimes, int timeOut){
            char com[255];
            char png[50];
            char tmp[50];
            strcpy(png, filename);
            strcat(png, ".png");
            strcpy(com, "sudo wget -q 'http://api.map.baidu.com/staticimage/v2?ak=");
            strcat(com, SERVER_AK);
            strcat(com, "&center=");
            sprintf(tmp, "%lf", cen_lo);
            strcat(com, tmp);
            strcat(com, ",");
            sprintf(tmp, "%lf", cen_la);
            strcat(com, tmp);
            strcat(com, "&width=");
            sprintf(tmp, "%d", width);
            strcat(com, tmp);
            strcat(com, "&height=");
            sprintf(tmp, "%d", height);
            strcat(com, tmp);
            strcat(com, "&zoom=");
            sprintf(tmp, "%d", zoom);
            strcat(com, tmp);
            strcat(com, "&dpiType=ph' -O ");
            strcat(com, png);
            strcat(com, " -t ");
            sprintf(tmp, "%d", tryTimes);
            strcat(com, tmp);
            strcat(com, " --timeout=");
            sprintf(tmp, "%d", timeOut);
            strcat(com, tmp);
            printf("调用百度LBS接口获取静态图 \n");
            printf("中心经度 %lf \n中心纬度 %lf \n", cen_lo, cen_la);
            return system(com);
        }
};

class baiduMapGetPath{
    private:
        double ori_lo, ori_la;
        double des_lo, des_la;
        char mode[10];
        char ori_c[20];
        char des_c[20];
        char ori[40];
        char des[40];
        bool LOLAset;
    public:
        baiduMapGetPath(){
            ori_lo = -1;
            ori_la = -1;
            des_lo = -1;
            des_la = -1;
            LOLAset = false;
        }
        void setOrigin(double lo, double la){
            ori_lo = lo;
            ori_la = la;
            LOLAset = true;
        }
        void setDestination(double lo, double la){
            des_lo = lo;
            des_la = la;
            LOLAset = true;
        }
        void setOrigin(const char *orip){
            strcpy(ori, orip);
        }
        void setDestination(const char *desp){
            strcpy(des, desp);
        }
        void setMode(int setmode){
            switch(setmode){
                case DRIVING:
                    strcpy(mode, "driving");
                    break;
                case WALKING:
                    strcpy(mode, "walking");
                    break;
                case TRANSIT:
                    strcpy(mode, "transit");
                    break;
                case RIDING:
                    strcpy(mode, "riding");
                    break;
                default:
                    break;
            }
        }
        void setCity(const char *ori, const char *des){
            strcpy(ori_c, ori);
            strcpy(des_c, des);
        }
        int getPath(const char* filename, int tryTimes, int timeOut){
            char com[2550];
            char xml[500];
            char tmp[500];
            strcpy(xml, filename);
            strcat(xml, ".xml");
            strcpy(com, "sudo wget -q -N 'http://api.map.baidu.com/direction/v1?mode=");
            strcat(com, mode);
            strcat(com, "&origin=");
            
            if(LOLAset){
                sprintf(tmp, "\"%lf", ori_lo);
                strcat(com, tmp);
                strcat(com, ",");
                sprintf(tmp, "%lf", ori_la);
                strcat(com, tmp);
                strcat(com, "\"&destination=\"");
                sprintf(tmp, "%lf", des_lo);
                strcat(com, tmp);
                strcat(com, ",");
                sprintf(tmp, "%lf\"", des_la);
                strcat(com, tmp);
            }else{
                strcat(com, ori);
                strcat(com, "&destination=");                strcat(com, tmp);
                strcat(com, des);
            }

            strcat(com, "&origin_region=");
            strcat(com, ori_c);
            strcat(com, "&destination_region=");
            strcat(com, des_c);
            strcat(com, "&output=xml&ak=");
            strcat(com, SERVER_AK);
            strcat(com, "' -O ");
            strcat(com, xml);
            strcat(com, " -t ");
            sprintf(tmp, "%d", tryTimes);
            strcat(com, tmp);
            strcat(com, " --timeout=");
            sprintf(tmp, "%d", timeOut);
            strcat(com, tmp);
            printf("调用百度LBS接口获取路径 \n");
            if(LOLAset){
                printf("起点经度 %lf \n起点纬度 %lf \n", ori_lo, ori_la);
                printf("终点经度 %lf \n终点纬度 %lf \n", des_lo, des_la);
            }else{
                printf("起点 %s \n",ori);
                printf("终点 %s \n",des); 
            }
            return system(com);
        }
};

#endif